package com.wyc.serviceMap.remote;

import com.wyc.internalCommon.constant.AmapConfigConstant;
import com.wyc.internalCommon.constant.DistrictLevelEnum;
import com.wyc.serviceMap.dto.DistrictDTO;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 调用高德api获取地区编码
 */
@Service
public class MapDistrictClient {

    @Value("${amap.key}")
    private String amapKey;

    @Autowired
    private RestTemplate restTemplate;

    public List<DistrictDTO> initDistrict(String keywords){
        // 拼接路径url
        StringBuilder urlBuilder = new StringBuilder();
        urlBuilder.append(AmapConfigConstant.DISTRICT_URL_PREFIX);
        urlBuilder.append("?");
        urlBuilder.append("keywords="+keywords);
        urlBuilder.append("&");
        urlBuilder.append("subdistrict=3");
        urlBuilder.append("&");
        urlBuilder.append("key="+amapKey);

        ResponseEntity<String> responseEntity = restTemplate.getForEntity(urlBuilder.toString(), String.class);
        String districtString = responseEntity.getBody();

        List<DistrictDTO> districtList = getDistrict(districtString);

        return districtList;
    }

    private List<DistrictDTO> getDistrict(String districtString) {
        ArrayList<DistrictDTO> list = new ArrayList<>();
        try {
            JSONObject jsonObject = JSONObject.fromObject(districtString);
            if (jsonObject.has(AmapConfigConstant.STATUS)){
                int status = jsonObject.getInt(AmapConfigConstant.STATUS);
                if (status == 1){
                    if (jsonObject.has(AmapConfigConstant.DISTRICTS)) {
                        list = getDistrictChilds(jsonObject, "0", list);
                    }
                }
            }
        }catch (Exception e){

        }
        return list;
    }

    private static ArrayList<DistrictDTO> getDistrictChilds(JSONObject jsonObject,String parentCode,ArrayList<DistrictDTO> list) {
        JSONArray jsonArray = jsonObject.getJSONArray(AmapConfigConstant.DISTRICTS);
        for (int i = 0; i < jsonArray.size(); i++) {
            DistrictDTO districtDTO = new DistrictDTO();
            JSONObject district = jsonArray.getJSONObject(i);
            String adcode = "";
            if (district.has(AmapConfigConstant.ADCODE)){
                adcode = district.getString(AmapConfigConstant.ADCODE);
                districtDTO.setAddressCode(String.valueOf(adcode));
            }

            if (district.has(AmapConfigConstant.NAME)){
                String name = district.getString(AmapConfigConstant.NAME);
                districtDTO.setAddressName(name);
            }

            if (district.has(AmapConfigConstant.LEVEL)){
                String level = district.getString(AmapConfigConstant.LEVEL);

                if (level.equals(AmapConfigConstant.STREET)){
                    continue;
                }

                for (DistrictLevelEnum value : DistrictLevelEnum.values()) {
                    if (level.equals(value.getMsg())){
                        districtDTO.setLevel(value.getCode());
                    }
                }

                districtDTO.setParentAddressCode(parentCode);
            }

            list.add(districtDTO);

            if (district.has(AmapConfigConstant.DISTRICTS)){
                getDistrictChilds(district,adcode,list);
            }
        }
        return list;
    }

}
